//
//  HandSignHelper.swift
//  ANOA
//
//  Created by 邱仙凯 on 2024/9/23.
//  Copyright © 2024 zwb. All rights reserved.
//

import UIKit

class HandSignHelper: NSObject {
    
    private override init() {}
    
    static let shared = HandSignHelper()
    var returnImg:((UIImage?)->())?
    
    func onSubmitBtn(_ signatureImg: UIImage?) {
        if let clo = returnImg {
            clo(signatureImg)
        }
    }
    
    func show(returnImg:@escaping(UIImage?)->()) {
        
        
        let nav = BaseNavigationController()
        let vc = SignatureViewController()
        vc.returnImg = returnImg
        nav.viewControllers = [vc]
       
         
        let topViewController = RouterUtils.currentTopViewController()
        topViewController!.present_FullScreen(to: nav)
        

    }
}


 
import UIKit

class DrawingView: UIView {
    
    private var path = UIBezierPath()
    private var previousPoint = CGPoint.zero

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            previousPoint = touch.location(in: self)
            path.move(to: previousPoint)
        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let currentPoint = touch.location(in: self)
            path.addLine(to: currentPoint)
            previousPoint = currentPoint
            setNeedsDisplay() // 重绘视图
        }
    }

    override func draw(_ rect: CGRect) {
        UIColor.black.setStroke()
        path.lineWidth = 2.0
        path.stroke()
    }

    func clear() {
        path.removeAllPoints()
        setNeedsDisplay()
    }

    func getImage() -> UIImage {
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, 0)
        self.layer.render(in: UIGraphicsGetCurrentContext()!)
        let signatureImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return signatureImage!
    }
}
 
import UIKit

class SignatureViewController: UIViewController {
    
    var drawingView: DrawingView!
    var returnImg:((UIImage?)->())?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        self.title = "手写签名"
        
        let rightBtn = UIButton.creat4Item(title: "返回") { _ in
           self.dismiss(animated: true, completion: nil)
       }
       let right = UIBarButtonItem(customView: rightBtn)
       self.navigationItem.leftBarButtonItem = right
        
        
        setupDrawingView()
        setupButtons()
    }

    func setupDrawingView() {
        drawingView = DrawingView(frame: self.view.bounds)
        drawingView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(drawingView)
        
        // 添加约束让drawingView覆盖整个视图
        NSLayoutConstraint.activate([
            drawingView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            drawingView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            drawingView.topAnchor.constraint(equalTo: view.topAnchor),
            drawingView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
    }

    func setupButtons() {
        let clearButton = UIButton(type: .system)
        clearButton.setTitle("清除", for: .normal)
        clearButton.addTarget(self, action: #selector(clearSignature), for: .touchUpInside)
        clearButton.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(clearButton)
        
        let saveButton = UIButton(type: .system)
        saveButton.setTitle("保存", for: .normal)
        saveButton.addTarget(self, action: #selector(saveSignature), for: .touchUpInside)
        saveButton.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(saveButton)
        
        clearButton.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(26)
            make.width.equalTo(60)
            make.height.equalTo(30)
            make.bottom.equalToSuperview().offset(-20)
        }
        saveButton.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-26)
            make.width.equalTo(60)
            make.height.equalTo(30)
            make.bottom.equalToSuperview().offset(-20)
        }
     
    }

    @objc func clearSignature() {
        drawingView.clear()
    }

    @objc func saveSignature() {
        self.dismiss(animated: true, completion: nil)

        let signatureImage = drawingView.getImage()
        if let clo = self.returnImg {
          clo(signatureImage)
        }
       
    }
    // 仅支持横屏
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .landscape
    }

    // 默认横屏方向
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return .landscapeRight
    }

 

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let appdelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
        appdelegate.allowRotation = true
        // 强制旋转屏幕到横屏
        forceLandscapeOrientation()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.allowRotation = false //禁止旋转
        // 恢复为竖屏
        forcePortraitOrientation()
    }
    
     
    // 强制横屏方向
    func forceLandscapeOrientation() {
        let value = UIInterfaceOrientation.landscapeRight.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")
        UINavigationController.attemptRotationToDeviceOrientation()
    }
    
    // 强制竖屏方向
    func forcePortraitOrientation() {
        let value = UIInterfaceOrientation.portrait.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")
        UINavigationController.attemptRotationToDeviceOrientation()
    }
}
 

